import { addProblemPieceApi } from "@/api/waybillManagement/customerServiceWaybill";
import { message } from "ant-design-vue";

export default function OperationHook(state: Base.GeneraDataType) {
  const { formDataRef, formState, emit, passData, getProblemOptions, loading } = state;
  const onSubmit = () => {
    formDataRef.value.validate().then(() => {
      //获取问题件类型ID
      const { id } = getProblemOptions.value.find((t: any) => t.label === formState.bizProblemPieceTypeId);
      const params = {
        ...formState,
        shipmenId: passData.value.id,
        bizProblemPieceTypeId: id,
        costList: formState.bizProblemPieceTypeId === "附加费" ? formState.costList.filter((t: any) => t.unitPrice) : undefined
      };
      if (params.costList && !params.costList.length && formState.bizProblemPieceTypeId === "附加费") return message.error("调整单价不能为0");

      loading.value = true;
      return addProblemPieceApi(params)
        .then(({ code }: any) => {
          if (code === 200) {
            message.success("操作成功");
            emit("submit");
          }
        })
        .finally(() => {
          loading.value = false;
        });
    });
  };

  const resetForm = () => {
    formDataRef.value.resetFields();
    emit("update:visible", false);
  };
  return {
    onSubmit,
    resetForm
  };
}
